package com.example.消息过期.ttlmessage;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;

@RestController
public class TTLMessage {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDealyMessage")
    public void send(String message){
        Message msg = MessageBuilder
            .withBody(message.getBytes(Charset.forName("UTF-8")))
            //给消息设置过期时间
            .setExpiration("5000")
            .build();
        rabbitTemplate.send(DelayConfig.DELAY_EXCHANGE, DelayConfig.DELAY_QUEUE,msg);
    }

}
